/* ###################################################################
**     Filename    : main.c
**     Project     : RGB
**     Processor   : MKL25Z128VLK4
**     Version     : Driver 01.01
**     Compiler    : GNU C Compiler
**     Date/Time   : 2017-03-02, 13:55, # CodeGen: 0
**     Abstract    :
**         Main module.
**         This module contains user's application code.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.01
** @brief
**         Main module.
**         This module contains user's application code.
*/         
/*!
**  @addtogroup main_module main module documentation
**  @{
*/         
/* MODULE main */


/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "PWM1.h"
#include "TU1.h"
#include "PWM2.h"
#include "TU2.h"
#include "PWM3.h"
#include "AS1.h"
/* Including shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"
/* User includes (#include below this line is not maintained by Processor Expert) */
#include "string.h"
#include "stdio.h"

LDD_TDeviceData* p_serial = NULL;
LDD_TDeviceData* p_red = NULL;
LDD_TDeviceData* p_blue = NULL;
LDD_TDeviceData* p_green = NULL;
int flag = 0;

void delay(int n)
{
	while(n--);
}

/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  /* Write your local variable definition here */

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  /* Write your code here */
  /* For example: for(;;) { } */

  p_serial = AS1_Init(NULL);
  p_red = PWM1_Init(NULL);
  p_blue = PWM2_Init(NULL);
  p_green = PWM3_Init(NULL);

	PWM1_SetRatio16(p_red, 0);
	PWM2_SetRatio16(p_blue, 0);
	PWM3_SetRatio16(p_green, 0);

	char buf[100] = {};

	while(1)
	{
		memset(buf, 0, sizeof(buf));
		int i = 0;
		while(1)
		{
		  AS1_ReceiveBlock(p_serial, buf+i, 1);
		  while(flag == 0);
		  flag = 0;
		  if(buf[i] == '\n')
			  break;
		  else
			  i++;
		}
		if(buf[0] == '1')
		{
			int value_red=0, value_green=0, value_blue=0;
			sscanf(buf, "%*d:%d:%d:%d#\n", &value_red, &value_green, &value_blue);
			PWM1_SetRatio16(p_red, value_red*655);
			PWM2_SetRatio16(p_blue, value_blue*655);
			PWM3_SetRatio16(p_green, value_green*655);
		}
	}




  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.5 [05.21]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
